<?php

namespace app\beautiful\controller\admin\setting;

use app\BaseController;
use app\common\model\shoppay\ShopAllinpayModel;
use app\common\model\shoppay\ShoppayTerminalModel;


class Shoppay extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $number= $this->request->param('number');
        $terminal_num = $this->request->param('terminal_num');
        $where=[];
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $order['a.id']='desc';
        if(!empty($number)){
            $where['number']=$number;
        }
        if(!empty($terminal_num)){
            $where['terminal_num']=$terminal_num;
        }
        $res_data=ShoppayTerminalModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $where['id']=$id;
        $res_data=ShoppayTerminalModel::info($where);
        return $this->success($res_data);
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $up['is_effect']=$is_effect;
        $res=ShoppayTerminalModel::up($up,$where);
        return $this->success();
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=ShoppayTerminalModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $res=ShoppayTerminalModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function edit(){
        $shop_id=$this->request->shopId;
        $id = $this->request->param('id',0);
        $number = $this->request->param('number',null);
        $type=$this->request->param('type',null);
        $terminal_num=$this->request->param('terminal_num',null);
        $is_effect=$this->request->param('is_effect',null);
        $terminal_address=$this->request->param('terminal_address',null);
        if($id==0){
            return $this->error('id错误');
        }
        $ShoppayTerminalModel= new ShoppayTerminalModel();
        $all_info=$ShoppayTerminalModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('数据不存在');
        $s=[];
        $s[]=['number','=',$number];
        $s[]=['terminal_num','=',$terminal_num];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['id','<>',$id];
        $s[]=['is_delete','=',0];
        $info=$ShoppayTerminalModel->where($s)->find();
        if(!empty($info)) return $this->error('数据重复');
        $up=[];
        if($number!=null) $up['number']=$number;
        if($type!=null) $up['type']=$type;
        if($terminal_num!=null) $up['terminal_num']=$terminal_num;
        if($terminal_address!=null) $up['terminal_address']=$terminal_address;
        if($is_effect!=null) $up['is_effect']=$is_effect;
        $where['id']=$id;
        $res_data=ShoppayTerminalModel::up($up,$where);
        return $this->success($res_data);
    }
    public function add(){
        $shop_id=$this->request->shopId;
        $number = $this->request->param('number',null);
        $type=$this->request->param('type',null);
        $terminal_num=$this->request->param('terminal_num',null);
        $is_effect=$this->request->param('is_effect',null);
        $terminal_address=$this->request->param('terminal_address',null);
        $ShoppayTerminalModel= new ShoppayTerminalModel();
        $s=[];
        $s[]=['number','=',$number];
        $s[]=['terminal_num','=',$terminal_num];
        $s[]=['shop_id','=',$shop_id];
        $s[]=['is_delete','=',0];
        $all_info=$ShoppayTerminalModel->where($s)->find();
        if(!empty($all_info)) return $this->error('数据重复');
        $insert=compact('shop_id','type','terminal_address','terminal_num','number');
        if($is_effect!==null) $insert['is_effect']=$is_effect;
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=ShoppayTerminalModel::insertGetId($insert);
        return $this->success($insert_id);
    }
    public function allinpay_info(){
        $where=[];
        $shop_id=$this->request->shopId;
        $where['shop_id']=$shop_id;
        $res_data=ShopAllinpayModel::info($where);
        return $this->success($res_data);
    }
    public function allinpay_edit(){
        $shop_id=$this->request->shopId;
        $name = $this->request->param('name',null);
        $merch_num=$this->request->param('merch_num',null);
        $ShopAllinpayModel= new ShopAllinpayModel();
        $all_info=$ShopAllinpayModel->where(['shop_id'=>$shop_id])->find();
        if(empty($all_info)){
            $insert=[];
            $insert['shop_id']=$shop_id;
            $insert['name']=$name;
            $insert['merch_num']=$merch_num;
            ShopAllinpayModel::insertGetId($insert);
        }else{
            $up=[];
            if(!empty($name)) $up['name']=$name;
            if(!empty($merch_num)) $up['merch_num']=$merch_num;
            if(!empty($up)){
                ShopAllinpayModel::up($up,['shop_id'=>$shop_id]);
            }
        }
        return $this->success();
    }
}

